//
//  NEMASignal.m
//  Portable Traffic Signal Assistor
//
//  Created by Hao Xu on 5/30/12.
//  Copyright (c) 2012 TTU. All rights reserved.
//

#import "NEMASignal.h"

@implementation NEMASignal

-(id) initWithCycleLength:(NSString *)cycleLength
{
    self=[super init];
    self.cycleLength=cycleLength;
    self.offset=0;
    self.signalName=@"New Signal";
    self.offsetBorE=@"Green Begin";
    self.offsetPhase=@"1";
    if ([cycleLength intValue]<20) {
        return self;
    }
    NSString *yellowTime=@"3";
    NSString *allRedTime=@"1";
    int split=[cycleLength intValue]/4;
    NSString *greenTime=[NSString stringWithFormat:@"%d",split-3-1];
    NSString *lastGreenTime=[NSString stringWithFormat:@"%d",[cycleLength intValue]-3*split-3-1];
    SignalRing * ring1=[[[SignalRing alloc] init] autorelease];
    [self.ringList addObject:ring1];
//    ring1.barrierList=[[[NSMutableArray alloc] init] autorelease];
    SignalBarrierGroup *ring1Barrier1=[[[SignalBarrierGroup alloc] init] autorelease];
    SignalBarrierGroup *ring1Barrier2=[[[SignalBarrierGroup alloc] init] autorelease];
    [ring1.barrierList addObject:ring1Barrier1];
    [ring1.barrierList addObject:ring1Barrier2];
    SignalPhase *phase=[[[SignalPhase alloc] init] autorelease];
    phase.phaseID=@"1";
    phase.phaseMovement=@"EBL";
    phase.yellowTime=yellowTime;
    phase.allRedTime=allRedTime;
    phase.greenTime=greenTime;
    phase.barrier=@"1";
    phase.position=@"1";
    [ring1Barrier1.phaseList addObject:phase];
    phase=[[[SignalPhase alloc] init] autorelease];
    phase.phaseID=@"2";
    phase.phaseMovement=@"WBTR";
    phase.yellowTime=yellowTime;
    phase.allRedTime=allRedTime;
    phase.greenTime=greenTime;
    phase.barrier=@"1";
    phase.position=@"2";
    [ring1Barrier1.phaseList addObject:phase];
    phase=[[[SignalPhase alloc] init] autorelease];
    phase.phaseID=@"3";
    phase.phaseMovement=@"SBL";
    phase.yellowTime=yellowTime;
    phase.allRedTime=allRedTime;
    phase.greenTime=greenTime;
    phase.barrier=@"2";
    phase.position=@"1";
    [ring1Barrier2.phaseList addObject:phase];
    phase=[[[SignalPhase alloc] init] autorelease];
    phase.phaseID=@"4";
    phase.phaseMovement=@"NBTR";
    phase.yellowTime=yellowTime;
    phase.allRedTime=allRedTime;
    phase.greenTime=lastGreenTime;
    phase.barrier=@"2";
    phase.position=@"2";
    [ring1Barrier2.phaseList addObject:phase];
    
    SignalRing* ring2=[[[SignalRing alloc] init] autorelease];
    [self.ringList addObject:ring2];
//    self.ring2.phaseList=[[[NSMutableArray alloc] init] autorelease];
    SignalBarrierGroup * ring2Barrier1=[[[SignalBarrierGroup alloc] init] autorelease];
    SignalBarrierGroup * ring2Barrier2=[[[SignalBarrierGroup alloc] init] autorelease];
    [ring2.barrierList addObject:ring2Barrier1];
    [ring2.barrierList addObject:ring2Barrier2];
    phase=[[[SignalPhase alloc] init] autorelease];
    phase.phaseID=@"5";
    phase.phaseMovement=@"WBL";
    phase.yellowTime=yellowTime;
    phase.allRedTime=allRedTime;
    phase.greenTime=greenTime;
    phase.barrier=@"1";
    phase.position=@"1";
    [ring2Barrier1.phaseList addObject:phase];
    phase=[[[SignalPhase alloc] init] autorelease];
    phase.phaseID=@"6";
    phase.phaseMovement=@"EBTR";
    phase.yellowTime=yellowTime;
    phase.allRedTime=allRedTime;
    phase.greenTime=greenTime;
    phase.barrier=@"1";
    phase.position=@"2";
    [ring2Barrier1.phaseList addObject:phase];
    phase=[[[SignalPhase alloc] init] autorelease];
    phase.phaseID=@"7";
    phase.phaseMovement=@"NBL";
    phase.yellowTime=yellowTime;
    phase.allRedTime=allRedTime;
    phase.greenTime=greenTime;    
    phase.barrier=@"2";
    phase.position=@"1";
    [ring2Barrier2.phaseList addObject:phase];
    phase=[[[SignalPhase alloc] init] autorelease];
    phase.phaseID=@"8";
    phase.phaseMovement=@"SBTR";
    phase.yellowTime=yellowTime;
    phase.allRedTime=allRedTime;
    phase.greenTime=lastGreenTime;
    phase.barrier=@"2";
    phase.position=@"2";
    [ring2Barrier2.phaseList addObject:phase];
    
    return self;
}

@end
